<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>Layui</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<link rel="stylesheet" href="./layui/css/layui.css" media="all">
		<script type="text/javascript" src="js/global.js"></script>
		<script src="./layui/layui.js" charset="utf-8"></script>
		<!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
	</head>

	<body>
		<div hidden="hidden" class="layui-form-item" style="margin-top: 20px">
			<label class="layui-form-label">ID：</label>
			<div class="layui-input-inline" style="width: 350px">
				<input type="text" name="id" id="userid" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
			</div>
		</div>
		</div>
		<div id="demo" class="demo-tree-more"></div>
		<div class="layui-form-item">
			<div class="layui-input-block" style="margin-left:0;text-align:center;">
				<button class="layui-btn layui-btn-blue" id="formDemo">确认选择</button>
			</div>
		</div>
		<script>
			layui.use(['tree'], function() {
				$ = layui.$;
				var tree = layui.tree;
				//解决跨域
				$.ajaxPrefilter(function(options, originalOptions, jqXHR) {
					options.xhrFields = {
						withCredentials: true
					}
				})

				function getReole() {
					var userid = document.getElementById("userid").value; //当前角色id
					$.ajax({
						url: globalData.server + "./Modules/getAllModulesss", //拿到所有模块
						type: "post",
						datatype: "json",
						async: false,
						data: {
							"userId": userid //角色id传到后台
						},
						success: function(data1) {
							tree.render({ //tree.render() 方法指定一个元素，便可快速创建一个 tree 实例
								elem: '#demo', //传入元素选择器
								showCheckbox: true, //是否显示复选框
								data: data1, //获取节点数据
								id: 'id', //定义索引 
							});
						},
					});

					$("#formDemo").click(function() { //提交
						var checkData = tree.getChecked('id'); //获得选中的节点
						if(checkData.length == 0) { //判断复选框是否被选中
							layer.msg("请至少选择一个节点", {
								icon: 6
							});
							return false;
						} else {
							var ids = [];
							var chil = [];
							for(var key in checkData) { //循环父节点
								var node = checkData[key];
								ids.push(node.id); //在父节点末尾添加一个或多个元素
								chil = node.children; //获取子节点数组
								for(var key in chil) { //循环子节点
									var children = chil[key];
									ids.push(children.id); //在子节点末尾添加一个或多个元素
								}
							}
							$.ajax({
								url: globalData.server + './ModulesRoles/addModuleByIdRoles', //根据角色id添加模块
								type: "POST",
								datatype: "json",
								async: false,
								data: {
									"roleId": userid,
									'ids': JSON.stringify(ids),
								}, //传数据给后台
								success: function(result) {
									if(result.count > 0) {
										layer.msg(result.msg, {
											icon: 6
										});
										setTimeout(function() { //延迟加载
											var index = parent.layer.getFrameIndex(window.name);
											parent.layer.close(index); //关闭当前页
										}, 2000);

									} else {
										layer.msg(result.msg, {
											icon: 6
										});
									}
								}
							});
						}
					});

				}
				setTimeout(function() {
					getReole();
				}, 500);
			});
		</script>

	</body>

</html>